﻿package com.boyaa.shells.tool
{
	import com.adobe.images.PNGEncoder;
	import com.boyaa.debug.Debug;
	import com.boyaa.shells.control.GlobalVar;
	import com.boyaa.shells.GlobalConfig;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * 加载单个图片到容器
	 * @author Heineken
	 */ 
	 
	public class ImgLoader
	{
		private var _conteiner:MovieClip = null;
		private var _ani:MovieClip = null;
		private var _loader:Loader = null;
		private var _conteinerWidth:Number = 0;
		private var _conteinerHeight:Number = 0;
		private var _url:String = "";
		private var _loadCount:int = 0;
		
		public function ImgLoader(conteiner:MovieClip,ani:MovieClip) 
		{
			_conteiner = conteiner;
			_ani = ani;	
			_conteinerWidth = _conteiner.width;
			_conteinerHeight = _conteiner.height;
		}
		
		/**
		 * 加载图片
		 * */
		public function loadImg(url:String):void
		{
			_loadCount = 1;
			removeLoader();
			if (url == null)
			{
				_ani.visible = false;
				_ani.stop();
				return;
			}
			_url = url.replace(GlobalVar.instance.REMOVE_BLANK, "");
			if (_url != "")
			{
				if (!GlobalConfig.PHPDebug && _url.search(GlobalVar.instance.URL_HTML) == -1)
				{
					_ani.visible = false;
					_ani.stop();
					return;
				}
				_ani.visible = true;
				_ani.play();
				_loader  = new Loader();
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImgComplete);
				_loader.load(new URLRequest(_url));
			}
			else
			{
				_ani.visible = false;
				_ani.stop();
				return;
			}
		}
		
		
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			removeLoader();
			if (_loadCount > 3)
			{
				_loadCount = 0;
			}
			else
			{
				_loadCount++;
				_loader  = new Loader();
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImgComplete);
				_loader.load(new URLRequest(_url));
			}
        }
		
		public function removeLoader():void
		{
			if (_loader != null) 
			{
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImgComplete);
				_loader.unload();
				_loader = null;
			}
		}
		
		/**
		 * 加载完成
		 * */
		private function onLoadImgComplete(e:Event):void 
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImgComplete);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_ani.visible = false ;
			_ani.stop();
			if (_conteiner)
			{
				_conteiner.addChild(_loader);	
				_loader.width = _conteinerWidth;
				_loader.height = _conteinerHeight;
			}
			_loader = null;
			_loadCount = 0;
		}
	}
}